package com.lg.partition12;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/4/20 10:16
 */
public class P1113杂务 {

    private static final int N = 10010, M = 100100;
    private static int[] h = new int[N], e = new int[M], ne = new int[M], din = new int[N], hw = new int[N];
    private static int[] q = new int[N], f = new int[N];
    private static int n, idx = 0;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        Arrays.fill(h, -1);
        for (int i = 1; i <= n; i++) {
            int s = nextInt();
            hw[i] = nextInt();
            while (true) {
                int u = nextInt();
                if (u == 0) break;
                add(u, i);
            }
        }
        writer.println(topsort());
        writer.flush();
    }

    private static int topsort() {
        int hh = 0, tt = -1;
        for (int i = 1; i <= n; i++) {
            if (din[i] == 0) {
                q[++tt] = i;
                f[i] = hw[i];
            }
        }
        int ans = 0;
        while (hh <= tt) {
            int u = q[hh++];
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                f[v] = Math.max(f[v], f[u] + hw[v]);
                ans = Math.max(ans, f[v]);
                din[v]--;
                if (din[v] == 0) q[++tt] = v;
            }
        }
        return ans;
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
